if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000) ;

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

function task() {
  var 我的 = text('我的').findOne(20000)
  if (我的) {
    sleep(time)
    我的.parent().parent().click()
    sleep(time)
    var 小任务 = text('小任务').findOne(5000)
    if (小任务) {
      小任务.parent().click()
      sleep(time)
      var 去签到 = text('去签到').findOne(5000)
      if (去签到) {
        去签到.click()
        utils.updateAppProgress(50);
        sleep(time)
        var 块签到 = text('块签到').findOne(5000)
        if (块签到) {
          sleep(time)
          var 签到抽奖 = className('android.widget.ImageView').depth(13).drawingOrder(3).findOne(5000)
          if (签到抽奖) {
            点击控件(签到抽奖)
            utils.updateAppProgress(100);
            sleep(7000)
          }
        }
      }
    }
  }
}

function main() {
  let open_app = launchAppEnhance("i链");
  if (open_app) {
    task()
  }
  else {
    toastLog("i链打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
